03. Demo: Backtest 2

Part 1

Cd13650 C5 L4 Demo 2 V3

Enhancing the Training Code for Backtesting

  1. Recap and Setup:

    • Previous code for data processing and some training functionalities was set up.
    • An inverse transform was introduced, crucial for today's backtesting enhancements.
  2. Data Frame for Trade Tracking:

    • Initialize an empty Pandas DataFrame with columns: buy price, buy timestamp, sell price, sell timestamp.
    • Optional extra columns can be added for advanced metrics.
  3. Post Analysis Statistics:

    • Calculate metrics like maximum drawdown and win-loss ratio using buy and sell prices.
    • Additional metrics for filtering include volume, 20-day moving average, and 20-day standard deviation.
  4. Backtest Loop Enhancements:

    • Use inverse transform to convert normalized prices back to dollar values.
    • Adjust for sell price, using proper indexers.
    • Ensure all prices are extracted and converted accurately using the normalizer close.
  5. Trade Tracking Enhancements:

    • Track trades by referencing original datasets for dates, volume, and other indexed metrics.
  6. Graphical Representation:

    • Normalize and transform dataset values to plot true values for better interpretation.
    • Display winning against losing trades for analysis.

Part 2

Cd13650 C5 L4 Demo 2b V3

Analyzing Backtest Results Part 1

  1. Data Preparation

    • Create a trade profit column in the DataFrame by subtracting the buy price from the sell price.
    • Introduce winner and loser columns based on whether the trade profit is positive or negative.
  2. Verification

    • Ensure calculations reflect accurate trade results by confirming buy and sell price logic.
  3. Post Analysis Statistics

    • Maximum Drawdown: Determine the worst trade by finding the smallest (most negative) value in the trade profit column.
    • Win-Loss Ratio: Calculate the ratio of winning to losing trades by counting true values in the winner and loser columns.
  4. Visualizing Results

    • Use Seaborn's histplot function to graphically represent the data.
    • Plot winners and losers based on buy volume, adding aesthetic parameters like transparency and trend lines for better clarity.

This approach helps efficiently analyze trading strategies, offering insights for optimization.

Part 3

Cd13650 C5 L4 Demo 2c V3

Analyzing Backtest Results Part 2

Chart Analysis:

  • Objective: Understand trade charts to apply effective filters for a trading strategy.
  • Observations: Current charts lack clear separation between winners and losers, making it challenging to determine effective filters.
  • Data Insight: Both winning and losing trades show similar trends, requiring deeper analysis to identify filters.

Filtering Trade Data:

  • Volume Analysis: Attempt to filter trades based on volume was insufficient, with only two trades labeled as winners.
  • MA 20 and Standard Deviation 20: Explore alternative variables like moving average and standard deviation to create potential filters.

Recommendations for Filter Application:

  • Identify Patterns: Look for distinct peaks or valleys in trade charts that indicate a larger number of winners or losers.
  • Adjust Test Period: Always apply potential filters to a different time period to validate effectiveness and avoid bias.
  • Iterate and Test: Continuously refine strategies and test with new data to ensure robust trading strategies.

Course Objectives:

  • Focus on reinforcement learning for automated trading bots with emphasis on risk limitation.
  • Encourage learners to add stop losses and take profits as part of a comprehensive trading strategy.